package com.kitkat.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kitkat.entities.Project;
import com.kitkat.service.ProjectService;
import com.kitkat.service.UserService;

/**
 *
 * UserController
 * 
 * lixueqi
 * lixueqi
 * 2015年5月30日 下午3:36:46
 * 
 * @version 1.0.0
 *
 */
@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserService userService;
	@Autowired
	private ProjectService projectService;
	
	
	@RequestMapping(value="",method=RequestMethod.POST)
	public @ResponseBody Map<String,Object> registerUser(HttpServletRequest request,String email,String password){
		Map<String,Object> result = this.userService.registerUser(email, password);
		return result;
	}
	
	@RequestMapping(value="/login",method=RequestMethod.POST)
	public @ResponseBody Map<String,Object> userLogin(String email,String password){
		Map<String,Object> hm = this.userService.userLogin(email, password);
		return hm;
	}
	
	@RequestMapping(value="/active",method=RequestMethod.GET)
	public String activeUser(String email,String activeCode,Map<String,Object> model){
		String result = this.userService.activeUser(email, activeCode);
		model.put("result", result);
		return "login";
	}
	
	@RequestMapping(value="/projects",method=RequestMethod.GET)
	public @ResponseBody Map<String,Object> getProjects(String token){
		Map<String,Object> result = this.projectService.getProjectByToken(token);
		Object obj = result.get("result");
		List<Project> list = null;
		if(obj!=null){
			list = (List<Project>)obj;
			for(int i=0;i<list.size();i++){
				list.get(i).setUser(null);
			}
		}
		result.put("result", list);
		return result;
	}
}
